/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import controller.AddTimeTable;
import controller.LabDetail;
import controller.ListLabs;
import controller.ListUsers;
import controller.UserDetail;
import entities.Lab;
import entities.TimeTable;
import entities.Users;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author QUY-THI
 */
@ManagedBean
@ViewScoped
public class AddTimeTableBean {

    private List<Lab> labs;
    private List<Users> users;
    private int labid;
    private int userid;
    private Date start;
    private Date end;
    private String description;

    public List<Lab> getLabs() {
        ListLabs l = new ListLabs();
        List<Lab> la = l.getLabs();
        List<Lab> ls = new ArrayList<Lab>();
        for (Lab lb : la) {
            if (lb.getStatus()) {
                ls.add(lb);
            }
        }
        return ls;
    }

    public void setLabs(List<Lab> labs) {
        this.labs = labs;
    }

    public List<Users> getUsers() {
        ListUsers u = new ListUsers();
        List<Users> us = new ArrayList<Users>();
        List<Users> ue = u.getUsers();
        for (Users ur : ue) {
            if (ur.getStatus()) {
                us.add(ur);
            }
        }
        return us;
    }

    public void setUsers(List<Users> users) {
        this.users = users;
    }

    public int getLabid() {
        return labid;
    }

    public void setLabid(int labid) {
        this.labid = labid;
    }

    public int getUserid() {
        return userid;
    }

    public void setUserid(int userid) {
        this.userid = userid;
    }

    public Date getStart() {
        return start;
    }

    public void setStart(Date start) {
        this.start = start;
    }

    public Date getEnd() {
        return end;
    }

    public void setEnd(Date end) {
        this.end = end;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * Creates a new instance of AddTimeTableBean
     */
    public AddTimeTableBean() {
    }

    public String resultAdd() {
        TimeTable tt = new TimeTable();
        LabDetail l = new LabDetail();
        UserDetail u = new UserDetail();
        tt.setDescription(description);
        tt.setEndTime(end);
        tt.setLabID(l.labDetail(labid));
        tt.setStartTime(start);
        tt.setUserID(u.getUserDetail(userid));
        AddTimeTable at = new AddTimeTable();
        boolean addTimeTable = at.addTimeTable(tt);
        if (addTimeTable) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "", "Success"));
            return "";
        }
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Error please try again later"));
        return "";
    }
}
